package com.sbr.esbmonitor.camel.route;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sbr.esbmonitor.camel.model.Endpoint;

public class EndpointParser {

	public EndpointParser() {
	}

	public List<Endpoint> parse(String... endpoints) {
		List<Endpoint> endpointList = new ArrayList<Endpoint>();
		for (String endpoint : endpoints) {
			endpointList.add(parse(endpoint));
		}
		return endpointList;
	}

	public Endpoint parse(String endpoint) {
		try {
			URI uri = new URI(endpoint);
			String componentName = uri.getScheme();
			System.out.println("componentName : "+componentName);
			String name = uri.getAuthority();
			System.out.println("name : "+name);
			Map<String, String> parameters = new HashMap<String, String>();
			String query = uri.getQuery();
			System.out.println("query : "+query);
			if (query != null) {
				for (String keyValuePair : query.split("&")) {
					System.out.println(keyValuePair);
					// TODO defensive coding
					String[] keyValueArray = keyValuePair.split("=");
					parameters.put(keyValueArray[0], keyValueArray[1]);
				}
			}

			return new Endpoint (componentName, name, parameters);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return null;

	}

	@Deprecated
	public Endpoint parse3(String endpoint) {
		String componentName = "";

		Pattern COMPONENT_NAME_PATTERN = Pattern.compile("^(.*?):");

		Matcher m = COMPONENT_NAME_PATTERN.matcher(endpoint);
		while (m.find()) {
			componentName = m.group(1);
			System.out.println("componentName : "+componentName);
		}

		String name = "";

		Pattern NAME_PATTERN = Pattern.compile("//(.*?)\\?");

		m = NAME_PATTERN.matcher(endpoint);
		while (m.find()) {
			name = m.group(1);
			System.out.println("name : "+name);
		}

		//	Pattern	PARAMETERS_PATTERN = Pattern.compile("*?");
		//		
		//		m = NAME_PATTERN.matcher(endpoint);
		//		while (m.find()) {
		//		    name = m.group();
		//		}
		//		
		Map<String, String> parameters = new HashMap<String, String>();



		return new Endpoint (componentName, name, parameters);

	}

	@Deprecated
	public Endpoint parse2(String endpoint) {
		String componentName = endpoint.substring(0, endpoint.indexOf(":"));
		String name = null;
		Map<String, String> parameters = new HashMap<String, String>();

		if (endpoint.contains("?")) {
			name = endpoint.substring(endpoint.indexOf("//")+2, endpoint.indexOf("?"));
			String parametersString = endpoint.substring(endpoint.indexOf("?")+1);
			System.out.println(parametersString);
			for (String keyValuePair : parametersString.split("&")) {
				System.out.println(keyValuePair);
				// TODO defensive coding
				String[] keyValueArray = keyValuePair.split("=");
				parameters.put(keyValueArray[0], keyValueArray[1]);
			}
		} else {
			//TODO no parameters
			name = endpoint.substring(endpoint.indexOf("//")+2);
		}

		return new Endpoint (componentName, name, parameters);

	}


}
